package com.ruoyi.mine.service.Impl;


import com.ruoyi.backend.util.CommmonFileUtils;
import com.ruoyi.backend.util.MinioUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exception.backendEnum.MineProductionException;

import com.ruoyi.mine.dto.ProductionInfoUpdateDTO;
import com.ruoyi.mine.dto.ProductionUploadDTO;
import com.ruoyi.mine.mapper.ProductionMapper;
import com.ruoyi.mine.pojo.Production;
import com.ruoyi.mine.service.ProductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Service
public class ProductionServiceImpl implements ProductionService {



    @Autowired
    private ProductionMapper productionMapper;

    @Autowired
    private MinioUtil minioUtil;

    @Value("${minio-bucket.productionfile}")
    private String minioBucketName;

    @Override
    public void upload(ProductionUploadDTO productionUploadDTO) throws IOException {

        MultipartFile file = productionUploadDTO.getFile();
        String orignalFileName = productionUploadDTO.getLayerName();
        String minioFileName = CommmonFileUtils.getMinioFileName(orignalFileName);
        productionUploadDTO.setMinioFileName(minioFileName);
        productionUploadDTO.setMinioBucketName(minioBucketName);

        // 文件存储;上传到Minio
        // 标准文件重命名来自前端传递
        // 调用封装的Minio工具类来上传矿山生产文件数据

        minioUtil.uploadFile(minioBucketName, minioFileName, file);
        //将记录上传到数据库表
        productionMapper.upload(productionUploadDTO);
    }

    @Override
    public void delete(Integer id) {
        if (productionMapper.getProductionCount(id) == 0){
            throw new ServiceException(MineProductionException.MINE_PRODUCTION_DATA_NOT_EXIST);
        }

        // 获取要删除的矿山生产数据
        Production production = productionMapper.findById(id);
        if (production != null) {
            String minioFileName = production.getMinioFileName();


            // 删除 Minio 中指定桶的文件

            boolean removed = minioUtil.removeFile(minioBucketName, minioFileName);
            if (!removed) {
                throw new ServiceException("删除 Minio 文件失败: " + minioFileName);
            }
        }
        productionMapper.delete(id);
    }

    @Override
    public void update(ProductionInfoUpdateDTO productionInfoUpdateDTO) {
        if (productionMapper.getProductionCount(productionInfoUpdateDTO.getProduceId()) == 0){
            throw new ServiceException(MineProductionException.MINE_PRODUCTION_DATA_NOT_EXIST);
        }
        productionMapper.update(productionInfoUpdateDTO);
    }

    @Override
    public List<Production> search(String mineId, String layerName, String produceType, LocalDate startTime, LocalDate endTime, String fileType) {

        List<Production> MineProductionList = productionMapper.search(mineId, layerName, produceType, startTime, endTime, fileType);

//        if (MineProductionList.isEmpty()){
//            throw new ServiceException(MineProductionException.MINE_PRODUCTION_DATA_NOT_FOUND);
//        }
        return MineProductionList;
    }
}
